package com.demo.java.OD1_50.OD8;

import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 按索引范围翻转文章片段问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145503087
 */
public class OdMain {

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            // 输入英文字符串、起始下标和结束下标
            String input = sc.nextLine();
            int start = sc.nextInt();
            int end = sc.nextInt();

            // 处理输入字符串
            String result = reverseWordsInRange(input, start, end);
            System.out.println(result);
        } catch (Exception e) {
            System.out.println("输入错误: " + e.getMessage());
        }
    }

    /**
     * 按指定范围翻转字符串中的单词顺序
     * @param input 输入的字符串
     * @param start 翻转的起始索引
     * @param end 翻转的结束索引
     * @return 处理后的字符串
     */
    public static String reverseWordsInRange(String input, int start, int end) {
        // 按照空格分割输入的字符串
        String[] words = input.split(" ");

        // 如果区间无效，返回 "EMPTY"
        if (words.length <= 1 || start >= words.length || end >= words.length || start > end) {
            return "EMPTY";
        }

        // 翻转指定区间的单词顺序
        List<String> wordList = new ArrayList<>(Arrays.asList(words));
        List<String> sublist = wordList.subList(start, end + 1);
        Collections.reverse(sublist);

        // 重新组合单词数组
        return String.join(" ", wordList);
    }
}
